{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T02:50:12.601276Z",
     "start_time": "2024-10-24T02:50:12.560844Z"
    }
   },
   "cell_type": "code",
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ],
   "id": "f7447ff06f95fab6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The autoreload extension is already loaded. To reload it, use:\n",
      "  %reload_ext autoreload\n"
     ]
    }
   ],
   "execution_count": 48
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T02:50:19.638080Z",
     "start_time": "2024-10-24T02:50:19.602861Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "\n",
    "import numpy as np\n",
    "import torch\n",
    "import pandas as pd\n",
    "from pandas.core.frame import DataFrame\n",
    "from torch.utils.data import Dataset, DataLoader\n",
    "from typing import List\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "from torch.nn.utils.rnn import pad_sequence\n",
    "import importlib"
   ],
   "id": "initial_id",
   "outputs": [],
   "execution_count": 49
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T02:50:20.808683Z",
     "start_time": "2024-10-24T02:50:20.802044Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "fae7221db5aafcee",
   "outputs": [],
   "execution_count": 49
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T02:50:22.579654Z",
     "start_time": "2024-10-24T02:50:21.088137Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data_file = './process/data/15_label_newgroup.csv'\n",
    "df = pd.read_csv(data_file)\n",
    "data = WindIcingDatasetV1(df, 64, 1, 10)\n",
    "\n",
    "print(len(data))"
   ],
   "id": "b8e0b95350657399",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "591\n"
     ]
    }
   ],
   "execution_count": 50
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T02:10:16.215182Z",
     "start_time": "2024-10-24T02:10:16.184178Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data_loader = DataLoader(data, batch_size=5, shuffle=False)\n",
    "for data, label in data_loader:\n",
    "    # 使用 pad_sequence 进行填充\n",
    "    print((data))\n",
    "    print(len(label))\n",
    "    break"
   ],
   "id": "8ff8adff48c1d44e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[tensor([[[ 1.8600,  1.2236,  2.5158,  ...,  1.3600,  0.0000,  1.5600],\n",
      "         [ 1.9116,  1.2934,  2.3136,  ...,  0.4400,  2.8800, -2.6000],\n",
      "         [ 1.6350,  1.2801,  2.5078,  ...,  1.7600,  0.6000,  2.5600],\n",
      "         ...,\n",
      "         [ 2.4243,  1.2635,  2.5038,  ...,  0.5200,  0.0000,  1.0000],\n",
      "         [ 1.9301,  1.2203,  2.5038,  ..., -0.7200,  0.0000, -1.9200],\n",
      "         [ 2.0813,  1.1937,  2.3636,  ...,  0.9600,  0.7600,  0.7200]],\n",
      "\n",
      "        [[ 2.3911,  1.2435,  2.5138,  ...,  2.8000,  0.3200, -0.0400],\n",
      "         [ 2.1034,  1.2369,  2.5158,  ...,  0.0800, -0.6400, -2.6800],\n",
      "         [ 2.2546,  1.2435,  2.5118,  ...,  0.1200, -3.2800,  1.2000],\n",
      "         ...,\n",
      "         [ 1.8452,  1.2867,  2.4037,  ..., -0.7200,  0.7600, -0.2000],\n",
      "         [ 1.5797,  1.3133,  2.3136,  ...,  0.3200,  1.9600, -0.4000],\n",
      "         [ 1.7973,  1.2302,  2.0993,  ..., -0.9600,  0.3600,  0.4400]],\n",
      "\n",
      "        [[ 2.3247,  1.3000,  2.5138,  ...,  0.1600,  0.4800, -1.8800],\n",
      "         [ 2.5496,  1.2934,  2.5078,  ...,  0.7200, -0.2400,  1.7200],\n",
      "         [ 2.5865,  1.2801,  2.5078,  ..., -1.5600, -0.7200,  2.7600],\n",
      "         ...,\n",
      "         [ 1.3437,  1.2867,  2.1574,  ...,  1.0000,  1.4000,  1.4000],\n",
      "         [ 1.3806,  1.2302,  2.1413,  ...,  0.4000,  0.4400,  0.0000],\n",
      "         [ 1.6977,  1.3233,  2.2375,  ...,  0.2000,  2.1600, -2.1600]],\n",
      "\n",
      "        [[ 1.9264,  1.2203,  2.0913,  ...,  0.5200,  0.7600,  1.0400],\n",
      "         [ 1.8268,  1.2735,  2.2855,  ...,  0.1200, -1.1200, -1.4400],\n",
      "         [ 1.9153,  1.2801,  2.4878,  ...,  0.5600,  3.2400,  1.4800],\n",
      "         ...,\n",
      "         [ 1.5539,  1.3133,  1.9731,  ..., -0.7200, -0.8800,  0.3200],\n",
      "         [ 1.5281,  1.2635,  2.1634,  ...,  0.8400,  1.5600, -0.8800],\n",
      "         [ 1.3179,  1.2435,  2.0933,  ...,  1.3600, -1.0800, -0.4000]],\n",
      "\n",
      "        [[ 2.1218,  1.2070,  2.5078,  ...,  0.6400,  0.9600,  1.4400],\n",
      "         [ 1.6719,  1.0873,  1.9851,  ...,  0.2800, -0.5600,  0.4800],\n",
      "         [ 1.4875,  1.2568,  2.1994,  ..., -0.6000,  0.5200, -0.9600],\n",
      "         ...,\n",
      "         [ 1.5723,  1.2735,  2.0472,  ...,  0.9200,  2.5600,  0.0800],\n",
      "         [ 1.5318,  1.2003,  1.6327,  ..., -1.2400, -0.7200,  0.4800],\n",
      "         [ 1.3105,  1.3000,  2.3436,  ..., -1.1600, -0.7600,  1.4000]]]), tensor([0, 0, 0, 0, 0], dtype=torch.int32)]\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 46
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T01:14:03.571463Z",
     "start_time": "2024-10-24T01:14:03.559320Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "\n",
    "# 假设有三条不等长的序列\n",
    "sequences = [torch.tensor([1, 2, 3]),\n",
    "             torch.tensor([4, 5]),\n",
    "             torch.tensor([6, 7, 8, 9])]\n",
    "\n"
   ],
   "id": "b6a221f860d5303a",
   "outputs": [],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T01:15:49.627139Z",
     "start_time": "2024-10-24T01:15:49.620879Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用 pad_sequence 进行填充\n",
    "padded_sequences = pad_sequence(sequences, batch_first=True)\n"
   ],
   "id": "331b289ad946fdb",
   "outputs": [],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T01:15:50.094229Z",
     "start_time": "2024-10-24T01:15:50.089176Z"
    }
   },
   "cell_type": "code",
   "source": "padded_sequences.shape",
   "id": "3c9bf5e4a6182b9f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 4])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T01:15:50.547675Z",
     "start_time": "2024-10-24T01:15:50.537675Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "# 创建 RNN\n",
    "rnn = nn.RNN(input_size=1, hidden_size=2, batch_first=True)\n",
    "\n",
    "# 将填充序列转换为合适的输入形状\n",
    "# 这里假设输入是单通道的\n",
    "padded_sequences = padded_sequences.unsqueeze(-1).float()\n",
    "print(padded_sequences)\n",
    "padded_sequences.shape"
   ],
   "id": "257594bfc75f568",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[1.],\n",
      "         [2.],\n",
      "         [3.],\n",
      "         [0.]],\n",
      "\n",
      "        [[4.],\n",
      "         [5.],\n",
      "         [0.],\n",
      "         [0.]],\n",
      "\n",
      "        [[6.],\n",
      "         [7.],\n",
      "         [8.],\n",
      "         [9.]]])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 4, 1])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T01:15:52.120528Z",
     "start_time": "2024-10-24T01:15:52.107368Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "# 前向传播\n",
    "output, hidden = rnn(padded_sequences)\n",
    "\n",
    "print(\"Padded Sequences:\")\n",
    "print(padded_sequences)\n",
    "print(\"RNN Output:\")\n",
    "print(output)"
   ],
   "id": "4c71d0536cc5ab0c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Padded Sequences:\n",
      "tensor([[[1.],\n",
      "         [2.],\n",
      "         [3.],\n",
      "         [0.]],\n",
      "\n",
      "        [[4.],\n",
      "         [5.],\n",
      "         [0.],\n",
      "         [0.]],\n",
      "\n",
      "        [[6.],\n",
      "         [7.],\n",
      "         [8.],\n",
      "         [9.]]])\n",
      "RNN Output:\n",
      "tensor([[[ 0.7139, -0.8048],\n",
      "         [ 0.6869, -0.8859],\n",
      "         [ 0.4474, -0.9588],\n",
      "         [ 0.9143, -0.5129]],\n",
      "\n",
      "        [[-0.2129, -0.9893],\n",
      "         [-0.4150, -0.9978],\n",
      "         [ 0.8780, -0.7651],\n",
      "         [ 0.9235, -0.3035]],\n",
      "\n",
      "        [[-0.7430, -0.9985],\n",
      "         [-0.8617, -0.9998],\n",
      "         [-0.9350, -0.9999],\n",
      "         [-0.9695, -1.0000]]], grad_fn=<TransposeBackward1>)\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T01:16:20.753214Z",
     "start_time": "2024-10-24T01:16:20.734214Z"
    }
   },
   "cell_type": "code",
   "source": "output.shape",
   "id": "d83b7ae6a20cca81",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 4, 2])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T07:27:13.156582Z",
     "start_time": "2024-10-24T07:27:13.121081Z"
    }
   },
   "cell_type": "code",
   "source": [
    "tmp = torch.zeros((2, 2, 2))\n",
    "tmp"
   ],
   "id": "6b6e93a2d7da43c6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0., 0.],\n",
       "         [0., 0.]],\n",
       "\n",
       "        [[0., 0.],\n",
       "         [0., 0.]]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T07:27:21.274559Z",
     "start_time": "2024-10-24T07:27:21.239560Z"
    }
   },
   "cell_type": "code",
   "source": [
    "tmp -= 1\n",
    "tmp"
   ],
   "id": "4598850fa5909b4b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-1., -1.],\n",
       "         [-1., -1.]],\n",
       "\n",
       "        [[-1., -1.],\n",
       "         [-1., -1.]]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T07:47:43.477197Z",
     "start_time": "2024-10-24T07:47:43.435224Z"
    }
   },
   "cell_type": "code",
   "source": [
    "tmp = torch.tensor(\n",
    "    [\n",
    "        [[1, 2], [3, 4], [5, 6]],\n",
    "        [[7, 8], [9, 10], [11, 12]],\n",
    "        [[13, 14], [15, 16], [17, 18]],\n",
    "    ]\n",
    ")\n",
    "tmp.shape"
   ],
   "id": "7c3e6f17890e293b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 3, 2])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 70
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T07:47:44.161691Z",
     "start_time": "2024-10-24T07:47:44.129865Z"
    }
   },
   "cell_type": "code",
   "source": "indices = torch.tensor([-1, 1, 2])",
   "id": "e97e1514c72bd310",
   "outputs": [],
   "execution_count": 71
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T07:48:50.579342Z",
     "start_time": "2024-10-24T07:48:50.550588Z"
    }
   },
   "cell_type": "code",
   "source": [
    "batch_indices = torch.arange(3, device=tmp.device)\n",
    "print(batch_indices)\n",
    "output_valid = tmp[batch_indices, indices]\n",
    "output_valid"
   ],
   "id": "5d28d301b1b988d8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([0, 1, 2])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "tensor([[ 5,  6],\n",
       "        [ 9, 10],\n",
       "        [17, 18]])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 73
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T07:35:57.080001Z",
     "start_time": "2024-10-24T07:35:57.041377Z"
    }
   },
   "cell_type": "code",
   "source": [
    "mask_index = torch.tensor([1, 2, -1])\n",
    "\n",
    "# 使用 gather 提取掩码位置的输出\n",
    "# 创建索引 tensor，形状为 (batch_size, 1) 用于 gather\n",
    "gather_indices = mask_index.unsqueeze(1)  # (batch_size, 1)\n",
    "print(gather_indices)\n",
    "# 使用 gather 提取\n",
    "output_valid = tmp.gather(1, gather_indices.expand(-1, tmp.size(2)))  # (batch_size, 1, class_num)\n",
    "print(output_valid)\n",
    "output_valid = output_valid.squeeze(1)  # (batch_size, class_num)\n",
    "print(output_valid)"
   ],
   "id": "931b1aada7e7ddc6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 1],\n",
      "        [ 2],\n",
      "        [-1]])\n"
     ]
    },
    {
     "ename": "RuntimeError",
     "evalue": "Index tensor must have the same number of dimensions as input tensor",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mRuntimeError\u001B[0m                              Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[57], line 8\u001B[0m\n\u001B[0;32m      6\u001B[0m \u001B[38;5;28mprint\u001B[39m(gather_indices)\n\u001B[0;32m      7\u001B[0m \u001B[38;5;66;03m# 使用 gather 提取\u001B[39;00m\n\u001B[1;32m----> 8\u001B[0m output_valid \u001B[38;5;241m=\u001B[39m \u001B[43mtmp\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mgather\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m1\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mgather_indices\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mexpand\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m-\u001B[39;49m\u001B[38;5;241;43m1\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mtmp\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43msize\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m2\u001B[39;49m\u001B[43m)\u001B[49m\u001B[43m)\u001B[49m\u001B[43m)\u001B[49m  \u001B[38;5;66;03m# (batch_size, 1, class_num)\u001B[39;00m\n\u001B[0;32m      9\u001B[0m \u001B[38;5;28mprint\u001B[39m(output_valid)\n\u001B[0;32m     10\u001B[0m output_valid \u001B[38;5;241m=\u001B[39m output_valid\u001B[38;5;241m.\u001B[39msqueeze(\u001B[38;5;241m1\u001B[39m)  \u001B[38;5;66;03m# (batch_size, class_num)\u001B[39;00m\n",
      "\u001B[1;31mRuntimeError\u001B[0m: Index tensor must have the same number of dimensions as input tensor"
     ]
    }
   ],
   "execution_count": 57
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-24T07:52:12.697968Z",
     "start_time": "2024-10-24T07:52:12.657579Z"
    }
   },
   "cell_type": "code",
   "source": [
    "if True:\n",
    "    aaa = 10\n",
    "else: aaa = 20\n",
    "\n",
    "print(aaa)"
   ],
   "id": "4f304b3cce3af15c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "execution_count": 74
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "from sklearn.model_selection import train_test_split",
   "id": "cad1f038dc5944d7"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
